{extend name="base" /}
{block name="body"}
<link rel="stylesheet" href="/static/dist/basic.min.css" />
<link rel="stylesheet" href="/static/dist/dropzone.min.css" />

<div class="layui-card" style="width: 1000px;height: 1600px">
    <div class="layui-card-body">
        <form action="{:request()->url()}" class="layui-form" method="post">
            <div class="layui-form-item">
                <label class="layui-form-label">机构编码</label>
                <div class="layui-input-block">
                    <input type="text" name="code" value="{$data.code|default=''}" autocomplete="off" placeholder="请输入机构编码" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">机构名称</label>
                <div class="layui-input-block">
                    <input type="text" name="name" value="{$data.name|default=''}" autocomplete="off" placeholder="请输入机构名称" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">保证金金额</label>
                <div class="layui-input-block">
                    <input type="text" name="deposit" value="{$data.deposit|default=''}" autocomplete="off" placeholder="请输入保证金" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">生效日期</label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" id="test1" name="effect_time" value="{$data.effect_time|default=''}" placeholder="{$data.effect_time|default=''}">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">失效日期</label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" id="test2" name="lose_time" value="{$data.lose_time|default=''}" placeholder="{$data.lose_time|default=''}">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">每单抽成</label>
                <div class="layui-input-block">
                    <input type="text" name="commision" value="{$data.commision|default=''}" autocomplete="off" placeholder="请输入抽成" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">车型</label>
                <div class="layui-input-block">
                    <input type="checkbox" title="摩托车" {if condition="isset($data) && strpos($data['type'],'0') >= 0"}checked{/if}>
                    <input type="checkbox" title="电动车" {if condition="isset($data) && strpos($data['type'],'1') >= 0"}checked{/if}>
                </div>
            </div>

            <input name="isedit" value="{:input('isedit')}" hidden="hidden">

            <input name="type" value="{$data.type|default=''}" hidden="hidden" id="type">

            {if condition="input('isedit') == 1"}
            <input value="{$data.id}" hidden="hidden" name="id">
            {/if}
        </form>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-filter="i" id="sub" >保存</button>
            </div>
        </div>

    </div>
</div>

<script type="text/javascript" src="/static/js/jquery.min.js"></script>
<script src="/static/js/clipboard.js"></script>
<script src="/static/dist/dropzone.min.js"></script>
<script type="text/javascript" charset="utf-8" src="/static/ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="/static/ueditor/ueditor.all.js"> </script >

{/block}

{block name="js"}
<script>
    $('#sub').click(function(){
        if($('input[name="name"]').val() == ''){
            layer.open({
                content: '机构名称必填' //这里content是一个普通的String
            });
            return;
        }

        if($('input[name="effect_time"]').val() == ''){
            layer.open({
                content: '生效时间必须填写' //这里content是一个普通的String
            });
            return;
        }

        if($('input[name="lose_time"]').val() == ''){
            layer.open({
                content: '失效时间必须填写' //这里content是一个普通的String
            });
            return;
        }
        if($('input[name="deposit"]').val() == ''){
            layer.open({
                content: '保证金额必须填写' //这里content是一个普通的String
            });
            return;
        }
        if($('input[name="commision"]').val() == ''){
            layer.open({
                content: '每单抽成必须填写' //这里content是一个普通的String
            });
            return;
        }
        if($('input[name="code"]').val() == ''){
            layer.open({
                content: '机构编码必须填写' //这里content是一个普通的String
            });
            return;
        }

        if($('input[type="checkbox"]').eq(0).is(':checked') == false && $('input[type="checkbox"]').eq(1).is(':checked') == false){
            layer.open({
                content: '车型必须勾选' //这里content是一个普通的String
            });
            return;
        }

        var str = [];
        if($('input[type="checkbox"]').eq(0).is(':checked')){
            str.push(0);
        }
        if($('input[type="checkbox"]').eq(1).is(':checked')){
            str.push(1);
        }



        $('#type').val(str.join(','));

        var data = $('form').serializeJson();
        $.ajax({
            url:'agency_add',
            data: data,
            method:'post',
            success:function(res){
                layer.open({
                    content: res.msg, //这里content是一个普通的String
                    end: function(){ //此处用于演示
                        if(res.code == 1){
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.location.reload();//刷新父页面，注意一定要在关闭当前iframe层之前执行刷新
                            parent.layer.close(index); //再执行关闭
                        }
                    }
                });


            }
        });
    })

    layui.use('laydate', function(){
        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#test1' //指定元素
        });

        laydate.render({
            elem: '#test2' //指定元素
        });
    });
</script>
{/block}
